/*
 * Copyright (c) 2025. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.starter.web.cc.data;

import cn.hallele.infrastructure.util.ObjectUtil;
import lombok.*;
import org.springframework.context.i18n.LocaleContextHolder;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典
 *
 * @author anle5
 * @since 2025/3/5 23:16
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DataDict implements Serializable {
    @Serial
    private static final long serialVersionUID = -8386653383258927684L;
    /**
     * 身份唯一标识
     */
    private Integer id;

    /**
     * 字典名字
     */
    private String name;

    /**
     * 字典key
     */
    private String code;

    /**
     * 字典状态
     */
    private String status;

    /**
     * 字典描述
     */
    private String description;

    /**
     * 字典项
     */
    private List<DataDictEntry> entries = new ArrayList<>();

    /**
     * 获取系统语言中的数据字典值
     *
     * @param key key
     * @return value
     */
    public String getDataDictValue(String key) {
        for (DataDictEntry entry : entries) {
            if (entry.getCode().equals(key)
                    && ObjectUtil.equals(LocaleContextHolder.getLocale().getLanguage(), entry.getLanguage())) {
                return entry.getValue();
            }
        }
        return null;
    }
}
